package com.chinawlx.wlxteacher.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chinawlx.wlxteacher.R;

import butterknife.ButterKnife;

/**
 * Created by yyc on 2016/8/2.
 */
public abstract class WLXBaseFragment extends Fragment {

    protected Context mContext;
    protected Resources mResources;
    private View mRootView;
    FragmentManager mFragmentManager;
    FragmentTransaction transaction;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext= context;
        mResources = mContext.getResources();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = createView(inflater, container, savedInstanceState);
        ButterKnife.bind(this,mRootView);

        return mRootView;
    }

    protected abstract View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    public void switchFragment(Fragment fragment) {
        mFragmentManager = getFragmentManager();
        transaction = mFragmentManager.beginTransaction();
        transaction.replace(R.id.fl_container, fragment);
        // 将页面添加到回退栈
        transaction.addToBackStack(null);
        transaction.commit();
    }
    /**
     * 打开一个Activity 默认 不关闭当前activity
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(clz, false, null);
    }


    public void startActivity(Class<?> clz,boolean isCloseCurrentActivity) {
        startActivity(clz, isCloseCurrentActivity, null);
    }

    public  void startActivity(Class<?> clz,boolean isCloseCurrentActivity,Bundle ex) {
        Intent intent=new Intent(getActivity(), clz);
        if(ex!=null)
            intent.putExtras(ex);
        startActivity(intent);
        if (isCloseCurrentActivity) {
            getActivity().finish();
        }
    }
}
